package v1

import (
	"adminHome/models"
	"adminHome/pkg/app"
	"adminHome/pkg/e"
	"adminHome/pkg/util"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"net/http"
)

func GetUserList(c *gin.Context) {
	appG := app.Gin{c}

	keyword := c.Query("query")

	maps := make(map[string]interface{})
	data := make(map[string]interface{})

	if keyword != "" {
		maps["username"] = keyword
	}
	code := e.SUCCESS
	data["list"] = models.GetUserList(util.GetPage(c), util.GetPageSize(c), maps)
	data["total"] = models.GetUserTotal(maps)

	appG.Response(http.StatusOK, code, data)

}

func GetUser(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()

	valid := validation.Validation{}
	valid.Required(id, "id").Message("id不能为空")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.ExistUserById(id) {
		code = e.ERROR_NOT_EXIST_USER
		appG.Response(http.StatusOK, code, nil)
		return
	}

	user, _ := models.GetUser(id)

	code = e.SUCCESS
	appG.Response(http.StatusOK, code, user)
}

func AddUser(c *gin.Context) {
	appG := app.Gin{c}

	username := c.PostForm("username")
	password := c.PostForm("password")

	valid := validation.Validation{}

	valid.Required(username, "username").Message("用户名为必填项")
	valid.Required(password, "password").Message("用户密码为必填项")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if models.ExistUserByName(username) {
		code = e.ERROR_EXIST_USERNAME
		appG.Response(http.StatusOK, code, nil)
		return
	}

	code = e.SUCCESS
	models.AddUser(username, password)
	appG.Response(http.StatusOK, code, nil)

}

func EditUser(c *gin.Context) {
	appG := app.Gin{c}

	id := com.StrTo(c.PostForm("id")).MustInt()
	statua := com.StrTo(c.PostForm("status")).MustInt()
	username := c.PostForm("username")
	password := c.PostForm("password")

	data := make(map[string]interface{})

	valid := validation.Validation{}
	valid.Required(id, "id").Message("id不能为空")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}
	if !models.ExistUserById(id) {
		code = e.ERROR_NOT_EXIST_USER
		appG.Response(http.StatusOK, code, nil)
		return
	}

	data["username"] = username
	data["password"] = password
	data["status"] = statua
	models.EditUser(id, data)
	code = e.SUCCESS
	appG.Response(http.StatusOK, code, nil)
}

func DeleteUser(c *gin.Context) {
	appG := app.Gin{c}

	id := com.StrTo(c.PostForm("id")).MustInt()
	valid := validation.Validation{}

	valid.Required(id, "id").Message("id为空")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.ExistUserById(id) {
		code = e.ERROR_NOT_EXIST_USER
		appG.Response(http.StatusOK, code, nil)
		return
	}
	models.DeleteUser(id)
	code = e.SUCCESS
	appG.Response(http.StatusOK, code, nil)
}
